﻿@page "/"

@implements IDisposable

@inject NavigationManager navigationManager
@inject ViewDataService viewdataService
@inject IUserStatusService statusService
@inject ISearchService searchService
@inject IUserFolderService folderService
@inject IPeopleService peopleService
@inject ITagSearchService tagService

<ImageBrowser/>

@code
{
    [ Parameter]
    [ SupplyParameterFromQuery]
    public string S { get; set; }

    [ Parameter]
    [ SupplyParameterFromQuery]
    public int FolderId { get; set; }

    [ Parameter]
    [ SupplyParameterFromQuery]
    public int TagId { get; set; }

    [ Parameter]
    [ SupplyParameterFromQuery]
    public string Tag { get; set; }

    [ Parameter]
    [ SupplyParameterFromQuery]
    public int PersonId { get; set; }

    [ Parameter]
    [ SupplyParameterFromQuery]
    public string Date { get; set; }

    private async Task ApplyQueryParams()
    {
        if( !string.IsNullOrEmpty(S) )
        {
            searchService.SearchText = S;
        }

        if( ! string.IsNullOrEmpty( Date ) )
        {
            if( DateTime.TryParse( Date, out var parsedDate) )
            {
                var start = parsedDate;
                var end = parsedDate.AddHours(24);

                statusService.UpdateStatus($"Selected date {parsedDate:dd-MMM-yyyy}");
                searchService.SetDateRange(start, end);
            }
        }

        if( FolderId != 0 )
        {
            var folder = await folderService.GetFolder(FolderId);

            if( folder != null )
            {
                statusService.UpdateStatus($"Selected folder {folder.Name}");
                searchService.Folder = folder;
            }
        }

        if( PersonId != 0 )
        {
            var person = await peopleService.GetPerson( PersonId );

            if( person != null )
            {
                statusService.UpdateStatus($"Selected person {person.Name}");
                searchService.Person = person;
            }
        }

        if( TagId != 0 )
        {
            var tag = await tagService.GetTag( TagId );

            if( tag != null )
                searchService.Tag = tag;
        }
        else if( !string.IsNullOrEmpty(Tag) )
        {
            // Search by text - this might return multiple results
            var matchingTags = await tagService.SearchTags(Tag);
            var tag = matchingTags.FirstOrDefault(x => x.Keyword.Equals(Tag, StringComparison.OrdinalIgnoreCase));

            if( tag != null )
                searchService.Tag = tag;
        }

        // Don't need this yet
        //this.UpdateQueryString(navigationManager);
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        viewdataService.SetSideBarState( new ViewDataService.SideBarState { ShowFolderList = true, ShowBasket = true, ShowTags = true } );

        navigationManager.LocationChanged += HandleLocationChanged;

        await ApplyQueryParams();
    }

    void HandleLocationChanged(object? sender, LocationChangedEventArgs e)
    {
        StateHasChanged();

        _ = ApplyQueryParams();
    }

    public void Dispose()
    {
        navigationManager.LocationChanged -= HandleLocationChanged;
    }
}